/**
 * Created by kangli on 2018/5/3.
 */
var sum;
var counts='';
var pageThis =0;
var pagecount = 10;
var index = 0;
var bannerid=''
$(function(){
    //得到用户列表
    getbannerLists();

    $("#file").change(function() {
        var files = this.files;
        $.each(files,function(i,e){
            var fileReader = new FileReader();
            fileReader.readAsDataURL(e);
            fileReader.onload = function(){
                $(".an-fengmianDiv").find("img").attr("src",this.result);
                /*subImg.find("#hrefimg").attr("href",this.result);*/
            }
        })
    });

    $("#file1").change(function() {
        var files = this.files;
        $.each(files,function(i,e){
            var fileReader = new FileReader();
            fileReader.readAsDataURL(e);
            fileReader.onload = function(){
                $(".changepic").attr("src",this.result);
                /*subImg.find("#hrefimg").attr("href",this.result);*/
            }
        })
    });



})
function getbannerLists(){
    var  data={page: index ,size: pagecount};
    AjaxSubmit("GET",juge(data), basePath + "/super/common/resource/searchBanner",searchBanner_fun);
}
function searchBanner_fun(res){
    console.log(res);
    if(res.status=='SUCCESS'){
        var userLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if (userLists.length != 0) {
            $("#userPage").css("display","block");
            if (sum % pagecount == 0) {
                counts = parseInt(sum / pagecount);
            }
            else {
                counts = parseInt(sum / pagecount) + 1;
            }
            $("#userPage").paging({
                pageNo: pageThis,
                totalPage: counts,
                totalSize: sum,
                callback: function (num) {
                    index = num - 1;
                    getbannerLists();
                    pageThis = num;
                }
            });
            $(".userTbody").empty();
            $('.warnDiv').remove();
            $.each(userLists, function (i, userList) {
                var type = '';
                var title = userList.title || '';
                var url = userList.url || '';
                if(userList.type=='FIRST_PAGE'){
                    type='首页'
                }else if(userList.type=='SCHOOL_COMMUNICATE'){
                    type='高校交流'
                }
                var html = '<tr> ' +
                    '<td class="texC"><img src="'+userList.pic+'"></td> ' +
                    '<td class="texC">'+title+'</td> ' +
                    '<td class="texC">'+url+'</td> ' +
                    '<td class="texC"> '+type+'</td> ' +
                    '<td class="texC"><span class=" cursorP  colorBlue"  data-id="'+userList.id+'"   data-toggle="modal" data-target="#editBanner" onclick="editBanner(this)" >编辑</span> | ' +
                    '<span class=" cursorP  colorBlue"  data-id="'+userList.id+'"  onclick="deleteBanner(this)">删除</span></td> ' +
                    '</tr>';
                $(".userTbody").append(html)
            })

        }
        else{
            $("#userPage").css("display","none");
            $(".userTbody").empty();
            $('.warnDiv').remove();
            var html = $('<div class="text-center warnDiv "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
            $(".userTable").after(html)
        }
    }
}

/*编辑轮播图*/
function editBanner(btn){
 /*  $("#editBanner").show();*/
      bannerid = $(btn).attr("data-id");
    $('#editBanner').on('shown.bs.modal', function () {
        var  data={bannerId: bannerid};
        console.log(data)
        AjaxSubmit("GET",juge(data), basePath + "/super/common/resource/getBannerById",getBannerById_fun);
        function getBannerById_fun(res){
            console.log(res);
             if(res.status=='SUCCESS'){
                 var title = res.obj.title || '';
                 var pic = res.obj.pic || '';
                 var url = res.obj.url || '';
                $(".bm-title1").val(title);
                 $(".changepic").attr("src",pic);
                 $(".bm-url1").val(url);
                 if(res.obj.type=='FIRST_PAGE'){
                     $(".accountType1").children("option").eq(0).attr("selected","selected")
                 }else if(res.obj.type=='SCHOOL_COMMUNICATE'){
                     $(".accountType1").children("option").eq(1).attr("selected","selected")
                 }
            }
        }
    });
}
function saveBanner(){
    var name = $(".bm-title1").val();
    var url = $(".bm-url1").val();
    var type=$(".accountType1").children("option:selected").text();
    console.log(type);
    var truetype='';
    if(type=='首页'){
        truetype='FIRST_PAGE'
    }else if(type=='高校交流'){
        truetype='SCHOOL_COMMUNICATE'
    }
    var question =(name!=""&&name!=undefined&& name!= null)?
        "UMtrue":confirm("标题不为空");


    if(question=='UMtrue'){
            var formData = new FormData();
            formData.append("id",bannerid);
            formData.append("file", $("#file1")[0].files[0]);
            formData.append("type", truetype);
            formData.append("title", name);
            formData.append("url", url);

            $.ajax({
                type : 'POST',
                url : basePath+'/super/common/resource/updateBanner',
                data : formData,
                processData : false,
                contentType : false,
                success : function(result) {
                    console.log(result)
                    index=$("#userPage").find(".active").find("a").text()-1;
                    getbannerLists();
                },
                error : function(err) {
                }
            });

            $("#editBanner").modal("hide")

    }
}
/*新增用户*/
function addBanner(btn){
    var name = $(".bm-title").val();
    var url = $(".bm-url").val();
    var type=$(".accountType").children("option:selected").text();
    var truetype='';
    if(type=='首页'){
        truetype='FIRST_PAGE'
    }else if(type=='高校交流'){
        truetype='SCHOOL_COMMUNICATE'
    }
    var question =(name!=""&&name!=undefined&& name!= null)?
        "UMtrue":confirm("标题不为空");


    if(question=='UMtrue'){
        if($("#file").val()!=''&&$("#file").val()!=null&&$("#file").val()!=undefined){
            var formData = new FormData();

            formData.append("file", $("#file")[0].files[0]);
            formData.append("type", truetype);
            formData.append("title", name);
            formData.append("url", url);

            $.ajax({
                type : 'POST',
                url : basePath+'/super/common/resource/addBanner',
                data : formData,
                processData : false,
                contentType : false,
                success : function(result) {
                    index=$("#userPage").find(".active").find("a").text()-1;
                    getbannerLists();
                    $("#file").val("");
                    $(".bm-title").val("");
                    $(".bm-url").val("");
                },
                error : function(err) {
                }
            });

            $("#addBanner").modal("hide");
        }else{
            alert("请先选择图片！")
        }

    }
}

/*删除用户*/
function deleteBanner(btn){
    var id = $(btn).attr("data-id");
    var msg = "您真的确定要删除吗？\n\n请确认！";
    if (confirm(msg)==true){
        var  data={bannerId: id};
        AjaxSubmit("POST",data, basePath + "/super/common/resource/deleteBanner",deleteBanner_fun);
        function deleteBanner_fun(res){
            if(res.status=='SUCCESS'){
                index=0;
                getbannerLists();
            }
        }
    }else{
        return false;
    }
}

//判断字符不空就存
function juge(obj){
    var result={};
    for(i in obj){
        if(obj[i]==null||obj[i]==""){
            continue;
        }
        var value=obj[i];
        eval("result."+i+"='"+value+"'");
    }
    return result;
}